package com.zpc.rabbitmq.reciever;

import java.io.IOException;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.amqp.RabbitProperties.Cache.Channel;
import org.springframework.stereotype.Component;

import com.rabbitmq.client.DefaultConsumer;
import com.zpc.rabbitmq.config.MqConfig;

@Component
public class Reciever {
	

	@RabbitHandler
	@RabbitListener(queues = { MqConfig.QUEUE_NAME })
	public void reciever(com.rabbitmq.client.Channel channel, Message message) {
		String json = new String(message.getBody());
		System.out.println("QUEUE_NAME队列收到信息：" + json);
		try {
			//限流 在非自动确认情况下，一定数目的消息没有确认，不进行消费新的消息
			// prefetchSize：消费消息的大小，0表示不做限制；
			// prefetchCount：同时消费消息的数量；
			// global：限流策略应用的级别，true：Channel级别；false：consumer级别；
			/*channel.basicQos(0, 1, false);*/
			// 消费者，要想做限流必须将自动ack设置为false
			/*channel.basicConsume(MqConfig.QUEUE_NAME,reciever);*/
			channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			try {
				channel.basicReject(message.getMessageProperties().getDeliveryTag(), false);
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}

	}

	@RabbitHandler
	@RabbitListener(queues = { MqConfig.QUEUE_TEST_NAME })
	public void reciever001(com.rabbitmq.client.Channel channel, Message message) {
		String json = new String(message.getBody());
		System.err.println("QUEUE_TEST_NAME队列收到信息：" + json);
		try {
			channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			try {
				channel.basicReject(message.getMessageProperties().getDeliveryTag(), false);
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}

	}

	@RabbitHandler
	@RabbitListener(queues= {MqConfig.QUEUE_TEST_FANOUT001} )
	public void recieverFanout01(com.rabbitmq.client.Channel channel,Message message) {
		String json = new String(message.getBody());
		System.err.println("QUEUE_TEST_FANOUT001队列收到信息：" + json);
		try {
			channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			try {
				channel.basicReject(message.getMessageProperties().getDeliveryTag(), false);
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
	}
	
	@RabbitHandler
	@RabbitListener(queues= {MqConfig.QUEUE_TEST_FANOUT002} )
	public void recieverFanout02(com.rabbitmq.client.Channel channel,Message message) {
		String json = new String(message.getBody());
		System.err.println("QUEUE_TEST_FANOUT002队列收到信息：" + json);
		try {
			channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			try {
				channel.basicReject(message.getMessageProperties().getDeliveryTag(), false);
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
	}
}
